home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / ncurses-5.3 / tack / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-27  |  6.9 KB  |  305 lines

  1. /*
  2. ** Copyright (C) 1991, 1997 Free Software Foundation, Inc.
  3. **
  4. ** This file is part of TACK.
  5. **
  6. ** TACK is free software; you can redistribute it and/or modify
  7. ** it under the terms of the GNU General Public License as published by
  8. ** the Free Software Foundation; either version 2, or (at your option)
  9. ** any later version.
  10. **
  11. ** TACK is distributed in the hope that it will be useful,
  12. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ** GNU General Public License for more details.
  15. **
  16. ** You should have received a copy of the GNU General Public License
  17. ** along with TACK; see the file COPYING.  If not, write to
  18. ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. ** Boston, MA 02111-1307, USA.
  20. */
  21. /* initialization and wrapup code */
  22.  
  23. #include <tack.h>
  24.  
  25. MODULE_ID("$Id: init.c,v 1.3 2001/06/16 17:54:19 tom Exp $")
  26.  
  27. #if NCURSES_VERSION_MAJOR >= 5 || NCURSES_VERSION_PATCH >= 981219
  28. #define _nc_get_curterm(p) _nc_get_tty_mode(p)
  29. #endif
  30.  
  31. FILE *debug_fp;
  32. char temp[1024];
  33. char tty_basename[64];
  34.  
  35. void
  36. put_name(const char *cap, const char *name)
  37. {                /* send the cap name followed by the cap */
  38.     if (cap) {
  39.         ptext(name);
  40.         tc_putp(cap);
  41.     }
  42. }
  43.  
  44. static void
  45. report_cap(const char *tag, const char *s)
  46. {                /* expand the cap or print *** missing *** */
  47.     int i;
  48.  
  49.     ptext(tag);
  50.     for (i = char_count; i < 13; i++) {
  51.         putchp(' ');
  52.     }
  53.     put_str(" = ");
  54.     if (s) {
  55.         putln(expand(s));
  56.     } else {
  57.         putln("*** missing ***");
  58.     }
  59. }
  60.  
  61.  
  62. void
  63. reset_init(void)
  64. {                /* send the reset and init strings */
  65.     int i;
  66.  
  67.     ptext("Terminal reset");
  68.     i = char_count;
  69.     put_name(reset_1string, " (rs1)");
  70.     put_name(reset_2string, " (rs2)");
  71.     /* run the reset file */
  72.     if (reset_file && reset_file[0]) {
  73.         FILE *fp;
  74.         int ch;
  75.  
  76.         can_test("rf", FLAG_TESTED);
  77.         if ((fp = fopen(reset_file, "r"))) {    /* send the reset file */
  78.             sprintf(temp, " (rf) %s", reset_file);
  79.             ptextln(temp);
  80.             while (1) {
  81.                 ch = getc(fp);
  82.                 if (ch == EOF)
  83.                     break;
  84.                 put_this(ch);
  85.             }
  86.             fclose(fp);
  87.         } else {
  88.             sprintf(temp, "\nCannot open reset file (rf) %s", reset_file);
  89.             ptextln(temp);
  90.         }
  91.     }
  92.     put_name(reset_3string, " (rs3)");
  93.     if (i != char_count) {
  94.         put_crlf();
  95.     }
  96.     ptext(" init");
  97.     put_name(init_1string, " (is1)");
  98.     put_name(init_2string, " (is2)");
  99.     if (set_tab && clear_all_tabs && init_tabs != 8) {
  100.         put_crlf();
  101.         tc_putp(clear_all_tabs);
  102.         for (char_count = 0; char_count < columns; char_count++) {
  103.             put_this(' ');
  104.             if ((char_count & 7) == 7) {
  105.                 tc_putp(set_tab);
  106.             }
  107.         }
  108.         put_cr();
  109.     }
  110.     /* run the initialization file */
  111.     if (init_file && init_file[0]) {
  112.         FILE *fp;
  113.         int ch;
  114.  
  115.         can_test("if", FLAG_TESTED);
  116.         if ((fp = fopen(init_file, "r"))) {    /* send the init file */
  117.             sprintf(temp, " (if) %s", init_file);
  118.             ptextln(temp);
  119.             while (1) {
  120.                 ch = getc(fp);
  121.                 if (ch == EOF)
  122.                     break;
  123.                 put_this(ch);
  124.             }
  125.             fclose(fp);
  126.         } else {
  127.             sprintf(temp, "\nCannot open init file (if) %s", init_file);
  128.             ptextln(temp);
  129.         }
  130.     }
  131.     if (init_prog) {
  132.         can_test("iprog", FLAG_TESTED);
  133.         (void) system(init_prog);
  134.     }
  135.     put_name(init_3string, " (is3)");
  136.  
  137.     fflush(stdout);
  138. }
  139.  
  140. /*
  141. **    display_basic()
  142. **
  143. **    display the basic terminal definitions
  144. */
  145. void
  146. display_basic(void)
  147. {
  148.     put_str("Name: ");
  149.     putln(ttytype);
  150.  
  151.     report_cap("\\r ^M (cr)", carriage_return);
  152.     report_cap("\\n ^J (ind)", scroll_forward);
  153.     report_cap("\\b ^H (cub1)", cursor_left);
  154.     report_cap("\\t ^I (ht)", tab);
  155. /*      report_cap("\\f ^L (ff)", form_feed);    */
  156.     if (newline) {
  157.         /* OK if missing */
  158.         report_cap("      (nel)", newline);
  159.     }
  160.     report_cap("      (clear)", clear_screen);
  161.     if (!cursor_home && cursor_address) {
  162.         report_cap("(cup) (home)", tparm(cursor_address, 0, 0));
  163.     } else {
  164.         report_cap("      (home)", cursor_home);
  165.     }
  166. #ifdef user9
  167.     report_cap("ENQ   (u9)", user9);
  168. #endif
  169. #ifdef user8
  170.     report_cap("ACK   (u8)", user8);
  171. #endif
  172.  
  173.     sprintf(temp, "\nTerminal size: %d x %d.  Baud rate: %ld.  Frame size: %d.%d", columns, lines, tty_baud_rate, tty_frame_size >> 1, (tty_frame_size & 1) * 5);
  174.     putln(temp);
  175. }
  176.  
  177. /*
  178. **    curses_setup(exec_name)
  179. **
  180. **    Startup ncurses
  181. */
  182. void
  183. curses_setup(
  184.     char *exec_name)
  185. {
  186.     int status;
  187.     static TERMTYPE term;
  188.     char tty_filename[2048];
  189.  
  190.     tty_init();
  191.  
  192.     /**
  193.        See if the terminal is in the terminfo data base.  This call has
  194.     two useful benefits, 1) it returns the filename of the terminfo entry,
  195.     and 2) it searches only terminfo's.  This allows us to abort before
  196.     ncurses starts scanning the termcap file.
  197.     **/
  198.     if ((status = _nc_read_entry(tty_basename, tty_filename, &term)) == 0) {
  199.         fprintf(stderr, "Terminal not found: TERM=%s\n", tty_basename);
  200.         show_usage(exec_name);
  201.         exit(1);
  202.     }
  203.     if (status == -1) {
  204.         fprintf(stderr, "Terminfo database is inaccessible\n");
  205.         exit(1);
  206.     }
  207.  
  208.     /**
  209.        This call will load the terminfo data base and set the cur-term
  210.     variable.  Only terminals that actually exist will get here so its
  211.     OK to ignore errors.  This is a good thing since ncurses does not
  212.     permit (os) or (gn) to be set.
  213.     **/
  214.     setupterm(tty_basename, 1, &status);
  215.  
  216.     /**
  217.        Get the current terminal definitions.  This must be done before
  218.     getting the baudrate.
  219.     **/
  220.     _nc_get_curterm(&cur_term->Nttyb);
  221.     tty_baud_rate = baudrate();
  222.     tty_cps = (tty_baud_rate << 1) / tty_frame_size;
  223.  
  224.     /* set up the defaults */
  225.     replace_mode = TRUE;
  226.     scan_mode = 0;
  227.     char_count = 0;
  228.     select_delay_type = debug_level = 0;
  229.     char_mask = (meta_on && meta_on[0] == '\0') ? ALLOW_PARITY : STRIP_PARITY;
  230.     /* Don't change the XON/XOFF modes yet. */
  231.     select_xon_xoff = initial_stty_query(TTY_XON_XOFF) ? 1 : needs_xon_xoff;
  232.  
  233.     fflush(stdout);    /* flush any output */
  234.     tty_set();
  235.  
  236.     go_home();    /* set can_go_home */
  237.     put_clear();    /* set can_clear_screen */
  238.  
  239.     if (send_reset_init) {
  240.         reset_init();
  241.     }
  242.  
  243.     /*
  244.        I assume that the reset and init strings may not have the correct
  245.        pads.  (Because that part of the test comes much later.)  Because
  246.        of this, I allow the terminal some time to catch up.
  247.     */
  248.     fflush(stdout);    /* waste some time */
  249.     sleep(1);    /* waste more time */
  250.     charset_can_test();
  251.     can_test("lines cols cr nxon rf if iprog rmp smcup rmcup", FLAG_CAN_TEST);
  252.     edit_init();            /* initialize the edit data base */
  253.  
  254.     if (send_reset_init && enter_ca_mode) {
  255.         tc_putp(enter_ca_mode);
  256.         put_clear();    /* just in case we switched pages */
  257.     }
  258.     put_crlf();
  259.     ptext("Using terminfo from: ");
  260.     ptextln(tty_filename);
  261.     put_crlf();
  262.  
  263.     if (tty_can_sync == SYNC_NEEDED) {
  264.         verify_time();
  265.     }
  266.  
  267.     display_basic();
  268. }
  269.  
  270. /*
  271. **    bye_kids(exit-condition)
  272. **
  273. **    Shutdown the terminal, clear the signals, and exit
  274. */
  275. void
  276. bye_kids(int n)
  277. {                /* reset the tty and exit */
  278.     ignoresig();
  279.     if (send_reset_init) {
  280.         if (exit_ca_mode) {
  281.             tc_putp(exit_ca_mode);
  282.         }
  283.         if (initial_stty_query(TTY_XON_XOFF)) {
  284.             if (enter_xon_mode) {
  285.                 tc_putp(enter_xon_mode);
  286.             }
  287.         } else if (exit_xon_mode) {
  288.             tc_putp(exit_xon_mode);
  289.         }
  290.     }
  291.     if (debug_fp) {
  292.         fclose(debug_fp);
  293.     }
  294.     if (log_fp) {
  295.         fclose(log_fp);
  296.     }
  297.     tty_reset();
  298.     fclose(stdin);
  299.     fclose(stdout);
  300.     fclose(stderr);
  301.     if (not_a_tty)
  302.         sleep(1);
  303.     exit(n);
  304. }
  305.